package com.awspaas.user.apps.processforsap.event;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListener;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListenerInterface;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.alipay.sofa.common.profile.StringUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

public class FI004CreateValidate extends InterruptListener implements InterruptListenerInterface {
    @Override
    public boolean execute(ProcessExecutionContext ctx) throws Exception {
        ProcessInstance processInstance = ctx.getProcessInstance();
        BO boMain = ctx.getBO("BO_EU_SAP_FIZC");
        String bukrs = boMain.getString("BUKRS");
        bukrs = bukrs.replace("00", "").replace("FH", "FE");

        List<BO> list = SDK.getBOAPI().query("BO_EU_SAP_FIZCLIST").bindId(processInstance.getId()).list();
        //获取资产类别
        List<BO> zcList = SDK.getBOAPI().query("BO_EU_FI_ZCLBWH").list();
        //获取品名
        List<BO> pmList = SDK.getBOAPI().query("BO_EU_FI_PMWH").list();
        //立项项目
        for (int i = 0; i < list.size(); i++) {
            BO bo = list.get(i);
            String anlkl = bo.getString("ANLKL");
            String kostl = bo.getString("KOSTL");
            String kostlv = bo.getString("KOSTLV");
            if (kostl.contains(bukrs)) {

            } else {
                throw new BPMNError("0331", "成本中心与所选公司代码不匹配，请核查！");
            }

            if (kostlv.contains(bukrs)) {

            } else {
                throw new BPMNError("0331", "使用部门与所选公司代码不匹配，请核查！");
            }

            List<BO> result1 = zcList.stream().filter(bo1 -> bo1.getString("KMDWM").equals(anlkl)).collect(Collectors.toList());
            if (result1 == null || result1.size() == 0) {
                throw new BPMNError("0331", "资产类别不匹配，请核查！");
            }
            //判断品名
            if (anlkl.equals("Z103") || anlkl.equals("Z420")) {
                String txt50 = bo.getString("TXT50");
                List<BO> result2 = pmList.stream().filter(bo1 -> bo1.getString("WLMC").equals(txt50)).collect(Collectors.toList());
                if (result2 == null || result2.size() == 0) {
                    throw new BPMNError("0331", "当资产类别为Z103或Z420时，品名需要进行匹配，请核查！");
                } else {
                    BO bo1 = result2.get(0);
                    String bindid = bo1.getBindId();
                    List<BO> list1 = SDK.getBOAPI().query("BO_EU_FI_XH").bindId(bindid).list();
                    String zxh = bo.getString("ZXH");
                    List<BO> list2 = list1.stream().filter(bo2 -> bo2.getString("XH").equals(zxh)).collect(Collectors.toList());
                    if (list2 == null || list2.size() == 0) {
                        throw new BPMNError("0331", "当品名为" + txt50 + "时，型号"+zxh+"无法匹配，请核查！");
                    }
                }
            }
            String iskztsy = bo.getString("ISKZTSY");
            if ("1".equals(iskztsy)) {
                if ("Z420/Z440".contains(anlkl)){
                    String kztsybm = bo.getString("ZZB");
                    if (StringUtil.isEmpty(kztsybm) ) {
                        throw new BPMNError("0331", "当【是否跨主体使用】选择是且资产类别为Z420/Z440时，站别字段必填！");
                    }
                }
            }
        }
        return true;
    }
}
